package com.moli.iov.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moli.iov.model.dto.req.PageConvert;
import com.moli.iov.model.dto.req.PageExtend;
import com.moli.iov.sys.mapper.RoleMapper;
import com.moli.iov.sys.model.dto.req.RoleDto;
import com.moli.iov.sys.model.po.Role;
import com.moli.iov.sys.service.IRoleService;
import com.moli.iov.util.CommonUtils;
import com.moli.iov.util.StringUtil;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 角色管理
 * </p>
 *
 * @author zql
 * @since 2019/9/6 9:48
 */
@Slf4j
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

  @Autowired
  private RoleMapper roleMapper;

  /**
   *
   * 查询角色列表
   *
   * @param roleDto
   * @return
   */
  @Override
  public List<Role> listRole(RoleDto roleDto) {
    return roleMapper.selectList(assemblyConditions(roleDto));
  }

  /**
   *
   * 分页查询角色列表
   *
   * @param roleDto
   * @return
   */
  @Override
  public PageExtend<Role> listRoleOfPage(RoleDto roleDto) {
    IPage<Role> roleIPage = roleMapper.selectPage(
        new Page<Role>(roleDto.getPageNum(), roleDto.getPageSize()),
        assemblyConditions(roleDto));
    return PageConvert.convert(roleIPage);
  }

  /**
   * 增加角色
   * @param roleDto
   * @return
   */
  @Override
  public Long saveRole(RoleDto roleDto) {
    Role role = new Role();
    CommonUtils.copyProperties(roleDto, role);
    roleMapper.insert(role);
    return role.getRoleId();
  }

  /**
   * 修改角色
   * @param role
   * @return
   */
  @Override
  public int updateRoleByRoleId(Role role) {
    QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("role_id", role.getRoleId());
    return roleMapper.update(role, queryWrapper);
  }

  /**
   * 根据角色id查询角色
   * @param roleId
   * @return
   */
  @Override
  public Role getRoleByRoleId(Long roleId) {
    return super.getById(roleId);
  }

  /**
   * 查询角色数量
   * @param roleDto
   * @return
   */
  @Override
  public int getRoleCount(RoleDto roleDto) {
    return super.count(assemblyConditions(roleDto));
  }


  /**
   * 组装查询条件
   * @param roleDto
   * @return
   */
  private QueryWrapper<Role> assemblyConditions(RoleDto roleDto){
    QueryWrapper<Role> queryWrapper = new QueryWrapper<>();

    if(roleDto == null){
      return queryWrapper;
    }

    if(roleDto.getRoleId() != null){
      queryWrapper.eq("role_id", roleDto.getRoleId());
    }else if(roleDto.getNeRoleId() != null){
      queryWrapper.ne("role_id", roleDto.getNeRoleId());
    }

    if(roleDto.getRoleIds() != null && !roleDto.getRoleIds().isEmpty()){
      queryWrapper.in("role_id", roleDto.getRoleIds());
    }

    if(roleDto.getCompanyId() != null){
      queryWrapper.eq("company_id", roleDto.getCompanyId());
    }

    if(StringUtil.isNotBlank(roleDto.getRoleName())){
      queryWrapper.like("role_name", roleDto.getRoleName());
    }

    if(StringUtil.isNotBlank(roleDto.getPerfectRoleName())){
      queryWrapper.eq("role_name", roleDto.getPerfectRoleName());
    }

    if(roleDto.getStatus() != null){
      queryWrapper.eq("status", roleDto.getStatus());
    }

    if(roleDto.getRoleType() != null){
      queryWrapper.eq("role_type", roleDto.getRoleType());
    }
    queryWrapper.orderByDesc("create_time");
    return queryWrapper;
  }
}
